﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Code.Models;
using Code.Commen;
using Code.BLL;

namespace QianBuo.SchemeTool.Forms
{
    public partial class SchemeForm : Form
    {
        public SchemeForm()
        {
            InitializeComponent();
            dgvScheme.AutoGenerateColumns = false;
            dgvScheme.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
        }

        private void SchemeForm_Load(object sender, EventArgs e)
        {
            InitSearch();
        }

        private void InitSearch()
        {
            cboLottery.Items.Clear();
            cboRatio.Items.Clear();
            cboState.Items.Clear();
            cboWinModel.Items.Clear();
            
            var lotterys = CacheManage.Lottery.Values
                //.Where(p => p.State == (short)LotteryState.Normal)
                .OrderBy(p => p.ID).OrderBy(p => p.Sort).Select(p => new { ID=(LotteryType)p.ID as LotteryType?, p.Name }).ToList();
            lotterys.Insert(0, new { ID = null as LotteryType?, Name = "所有彩种" });
            cboLottery.DisplayMember = "Name";
            cboLottery.ValueMember = "ID";
            cboLottery.DataSource = lotterys;

            var ratios = CacheManage.Ratio.Values
                //.Where(p => p.State == (short)ConsumeRatioState.Normal)
                .OrderBy(p => p.ID).Select(p => new { ID = p.Ratio as decimal?, p.Name }).ToList();
            ratios.Insert(0, new { ID = null as decimal?, Name = "所有模式" });
            cboRatio.DisplayMember = "Name";
            cboRatio.ValueMember = "ID";
            cboRatio.DataSource = ratios;

            var states = (Enum.GetValues(typeof(SchemeState)) as SchemeState[]).Select(p => new { Text = p.GetName(), Value = p as SchemeState? }).ToList();
            states.Insert(0, new { Text = "所有状态", Value = null as SchemeState? });
            cboState.DisplayMember = "Text";
            cboState.ValueMember = "Value";
            cboState.DataSource = states;

            var winmodels = CacheManage.WinModel.Values
                //.Where(p => p.State == (short)WinMoneyModelState.Normal)
                .OrderBy(p => p.ID).Select(p => new { ID = p.ID as int?, p.Name }).ToList();
            winmodels.Insert(0, new { ID = null as int?, Name = "所有模式" });
            cboWinModel.DisplayMember = "Name";
            cboWinModel.ValueMember = "ID";
            cboWinModel.DataSource = winmodels;
        }

        private void BinDate()
        {
            var SM = new SchemeManage();
            var search = GetFilter();
            if (search == null)
                return;

            var data = SM.GetSchemeDetail(search, null).OrderByDescending(p=>p.AddTime).ToArray() ;
            if (data.Length == 0)
                MessageBox.Show("无任何数据");
            else
                dgvScheme.DataSource = data;
        }

        private SchemeManage.SchemeSerchModel GetFilter()
        {
            var search = new SchemeManage.SchemeSerchModel();
            if (!string.IsNullOrEmpty(txtSchemeID.Text))
            {
                Guid id;
                try
                {
                    id = new Guid(txtSchemeID.Text.Trim());
                }
                catch { 
                    MessageBox.Show("方案ID输入不正确");
                    return null;
                }
                search.ID = id;
                return search;
            }
            if (!string.IsNullOrEmpty(txtUserName.Text))
                search.UserName = txtUserName.Text.Trim();
            if (!string.IsNullOrEmpty(txtIssueNumber.Text))
            {
                if (cboLottery.SelectedValue == null)
                {
                    MessageBox.Show("安期号查找要指定彩种");
                    return null;
                }
                var str = Issue.BulidIssueName((LotteryType)cboLottery.SelectedValue, txtIssueNumber.Text.Trim());
                search.IssueName = str;
            }

            search.LotteryType = cboLottery.SelectedValue as LotteryType?;
            search.Ratio = cboRatio.SelectedValue as decimal?;
            search.State = cboState.SelectedValue as SchemeState?;
            search.WinModel = cboWinModel.SelectedValue as int?;
            if (dateStartAddTime.Checked)
                search.StartTime = dateStartAddTime.Value.Date;
            if (dateEndAddTime.Checked)
                search.EndTime = dateEndAddTime.Value.Date.AddDays(1);
            return search;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BinDate();
        }
        
        private void cmCopy_Click(object sender, EventArgs e)
        {
            if (dgvScheme.GetCellCount(DataGridViewElementStates.Selected) > 0)
            {
                try
                {
                    // Add the selection to the clipboard.
                    Clipboard.SetDataObject(dgvScheme.GetClipboardContent());

                    // Replace the text box contents with the clipboard text.
                    //var str = Clipboard.GetText();
                }
                catch (System.Runtime.InteropServices.ExternalException ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
        }

        private void cmFilter_Click(object sender, EventArgs e)
        {
            var cell=dgvScheme.CurrentCell;
            if (cell != null)
            {
                var item = cell.OwningRow.DataBoundItem as SchemeManage.SchemeItemModel;
                switch (cell.OwningColumn.DataPropertyName)
                { 
                    case "SchemeID":
                        txtSchemeID.Text = item.SchemeID.ToString("N");
                        break;
                    case "UserName":
                        txtUserName.Text = item.UserName;
                        break;
                    case "IssueName":
                        cboLottery.SelectedValue = (LotteryType)item.LotteryID;
                        if (cboLottery.SelectedValue != null)
                            txtIssueNumber.Text = item.IssueName;
                        break;
                    case "LotteryName":
                        var val1 = cboLottery.SelectedValue;
                        cboLottery.SelectedValue = (LotteryType)item.LotteryID;
                        break;
                    case "AddTime":
                        dateEndAddTime.Value = item.AddTime.Date;
                        dateStartAddTime.Value = item.AddTime.Date;
                        break;
                    case "RatioName":
                        cboRatio.SelectedValue = item.Ratio;
                        break;
                    case "WinModelName":
                        cboWinModel.SelectedValue = item.WinModelID;
                        break;
                    case "StateName":
                        cboState.SelectedValue = (SchemeState)item.State;
                        break;
                }
            }
        }

        private void cmDetail_Click(object sender, EventArgs e)
        {
            var cell=dgvScheme.CurrentCell;
            if (cell != null)
            {
                var item = cell.OwningRow.DataBoundItem as SchemeManage.SchemeItemModel;
                var f = Application.OpenForms.Cast<Form>().Where(p => p is SchemeDetailForm).Cast<SchemeDetailForm>().FirstOrDefault(p => p.SchemeID == item.SchemeID);
                if (f == null)
                {
                    f = new SchemeDetailForm(item);
                    f.MdiParent = this.MdiParent;
                    f.Show();
                }
                f.Focus();
            }
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            var fs = new int[] { 0, 1, 2, 3, 4, 5, 7, 8 };
            cmFilter.Enabled = dgvScheme.CurrentCellAddress != null && fs.Contains(dgvScheme.CurrentCellAddress.X);
            cmCopy.Enabled = dgvScheme.SelectedCells.Count != 0;
            cmDetail.Enabled = dgvScheme.CurrentCell != null;
        }
    }
}
